package com.hushao.aimi.service.impl;
import com.hushao.aimi.ImagesUtil;
import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.entity.AimiProduct;
import com.hushao.aimi.vo.ProductSpecVo;
import com.hushao.aimi.vo.ProductVo;
import com.hushao.aimi.repository.ProductRespository;
import com.hushao.aimi.repository.ProductSpecRepository;
import com.hushao.aimi.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description: 商品服务
 * @author: 胡少
 * @create: 2018-06-08 11:19
 **/
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRespository productRespository;

    @Autowired
    private ProductSpecRepository productSpecRepository;

    @Override
    public ServerResponse detail(String productId) {
        Optional<AimiProduct> optional=productRespository.findById(productId);
        if (optional.isPresent()){
            AimiProduct ap=optional.get();
            List<ProductSpecVo> productSpecVos=productSpecRepository.findByProductId(productId)
                    .stream()
                    .map(pc->new ProductSpecVo(pc.getId(),pc.getName(),pc.getImage(),pc.getPrice(),pc.getStock())).collect(Collectors.toList());
            ProductVo productVo=new ProductVo(ap.getId(),ap.getName(),ap.getSubtitle(), ImagesUtil.segmentationImages(ap.getSubImages(),";"),ImagesUtil.segmentationImages(ap.getDetail(),";"),productSpecVos);
            return ServerResponse.Success(productVo);
        }

        return ServerResponse.Error("没有此商品！");
    }


}
